#include "biblereaderconfigurator.h"
#include <QFile>
#include <QDir>
#include <QUuid>
#include <QApplication>
#include <QFontDatabase>
#include "Logger.h"
BibleReaderConfigurator::BibleReaderConfigurator(QObject *parent) :
    QObject(parent)
{
    settings = nullptr;
    QDir dir(QApplication::applicationDirPath());
#ifdef Q_OS_OSX
    dir.cdUp();
    dir.cd("Resources");
#endif
    settingsFile = dir.absolutePath() + "/biblereader.ini";
    if (!QFile(settingsFile).exists()) {
        // create default settings
        settings = new QSettings(settingsFile, QSettings::IniFormat);
        settings->setValue("/base/layoutPath", dir.absolutePath() + "/layout/");
        settings->setValue("/base/biblePathBase", dir.absolutePath() + "/bibles/");
        settings->setValue("/base/dictPathBase", dir.absolutePath() + "/dicts/");
        settings->setValue("/base/bcPathBase", dir.absolutePath() + "/commentarys/");
        settings->setValue("/base/bxPathBase", dir.absolutePath() + "/xrefs/");
        settings->setValue("/base/bePathBase", dir.absolutePath() + "/ebooks/");
        settings->setValue("/base/defaultBibleVersion", "CUVS");
        settings->setValue("/base/defaultDict", "SNCHS");
        settings->setValue("/base/defaultCommentary", "DDESJZS");
        settings->setValue("/base/lastBook", "1");
        settings->setValue("/base/lastChapter", "1");
        settings->setValue("/base/lastVerse", "1");

        // bible text browser
        settings->setValue("/text/selectedVerseBG", "#7dde96");
        settings->setValue("/text/projectDlgBG", "#000000");
        settings->setValue("/text/projectDlgFG", "#ffffff");
        settings->setValue("/text/prjTextFontFamily", QFontDatabase::systemFont(QFontDatabase::TitleFont));
        settings->setValue("/text/prjTextFontSize", 28.0);

        settings->setValue("/text/bibleTextFontFamily",
                           QFontDatabase::systemFont(QFontDatabase::TitleFont));
        settings->setValue("/text/bibleTextFontSize", 16.0);

        settings->setValue("/bible/isShowXref", true);
        settings->setValue("/bible/verseCopyTitleStyle", 1);

        // commentary browser
        settings->setValue("/text/commTextFontFamily",
                           QFontDatabase::systemFont(QFontDatabase::TitleFont));
        settings->setValue("/text/commTextFontSize", 16.0);

        // tip text
        settings->setValue("/tip/tipTextFontFamily",
                           QFontDatabase::systemFont(QFontDatabase::TitleFont));
        settings->setValue("/tip/tipTextFontSize", 16.0);

        // app
        settings->setValue("/app/autoupdate", true);
        settings->setValue("/app/themename", "white");
        settings->setValue("/app/state", QByteArray());
        settings->setValue("/app/guid", QUuid::createUuid().toString());
        settings->setValue("/app/saysomethingtime", 0);
        settings->setValue("/app/clipboardmonitor", true);
    }
    if (!settings)
        settings = new QSettings(settingsFile, QSettings::IniFormat);

    layoutPath = settings->value("/base/layoutPath").toString();
    biblePathBase = settings->value("/base/biblePathBase").toString();
    dictPathBase = settings->value("/base/dictPathBase").toString();
    bcPathBase = settings->value("/base/bcPathBase").toString();
    bxPathBase = settings->value("/base/bxPathBase").toString();
    bePathBase = settings->value("/base/bePathBase", dir.absolutePath().append("/ebooks/")).toString();
    defaultBibleVersion = settings->value("/base/defaultBibleVersion").toString();
    defaultDict = settings->value("/base/defaultDict").toString();
    defaultCommentary = settings->value("/base/defaultCommentary").toString();
    lastBook = settings->value("/base/lastBook").toInt();
    lastChapter = settings->value("/base/lastChapter").toInt();
    lastVerse = settings->value("/base/lastVerse").toInt();
    if (lastBook == 0 || lastChapter == 0 || lastVerse == 0) {
        lastBook = 1; // gen
        lastChapter = 1;
        lastVerse = 1;
    }

    // bible text
    selectedVerseBG = QColor(settings->value("/text/selectedVerseBG").toString());
    projectDlgBG = QColor(settings->value("/text/projectDlgBG").toString());
    projectDlgFG = QColor(settings->value("/text/projectDlgFG").toString());
    prjTextFontFamily = settings->value("/text/prjTextFontFamily").toString();
    prjTextFontSize = settings->value("/text/prjTextFontSize").toDouble();
    bibleTextFontFamily = settings->value("/text/bibleTextFontFamily").toString();
    bibleTextFontSize = settings->value("/text/bibleTextFontSize").toDouble();
    commTextFontFamily = settings->value("/text/commTextFontFamily").toString();
    commTextFontSize = settings->value("/text/commTextFontSize").toDouble();
    tipTextFontFamily = settings->value("/tip/tipTextFontFamily").toString();
    tipTextFontSize = settings->value("/tip/tipTextFontSize").toDouble();
    isAutoUpdate = settings->value("/app/autoupdate").toBool();
    themeName = settings->value("/app/themename").toString();
    state = settings->value("/app/state").toByteArray();
    enableClipboardMonitor = settings->value("/app/clipboardmonitor", true).toBool();

    guid = settings->value("/app/guid").toString();
    if (guid.isEmpty()) {
        guid = QUuid::createUuid().toString();
    }
    saySomethingTime = settings->value("/app/saysomethingtime", 0).toInt();

    isShowXref = settings->value("/bible/isShowXref").toBool();
    verseCopyTitleStyle = settings->value("/bible/verseCopyTitleStyle").toInt();
}

BibleReaderConfigurator::~BibleReaderConfigurator()
{
    if (settings) {
        settings->setValue("/base/layoutPath", layoutPath);
        settings->setValue("/base/biblePathBase", biblePathBase);
        settings->setValue("/base/dictPathBase", dictPathBase);
        settings->setValue("/base/bcPathBase", bcPathBase);
        settings->setValue("/base/bxPathBase", bxPathBase);
        settings->setValue("/base/bePathBase", bePathBase);
        settings->setValue("/base/defaultBibleVersion", defaultBibleVersion);
        settings->setValue("/base/defaultDict", defaultDict);
        settings->setValue("/base/defaultCommentary", defaultCommentary);
        settings->setValue("/base/lastBook", QString::number(lastBook));
        settings->setValue("/base/lastChapter", QString::number(lastChapter));
        settings->setValue("/base/lastVerse", QString::number(lastVerse));

        // bible text settings
        settings->setValue("/text/selectedVerseBG", selectedVerseBG.name());
        settings->setValue("/text/projectDlgBG", projectDlgBG.name());
        settings->setValue("/text/projectDlgFG", projectDlgFG.name());
        settings->setValue("/text/prjTextFontFamily", prjTextFontFamily);
        settings->setValue("/text/prjTextFontSize", prjTextFontSize);
        settings->setValue("/text/bibleTextFontFamily", bibleTextFontFamily);
        settings->setValue("/text/bibleTextFontSize", bibleTextFontSize);
        settings->setValue("/text/commTextFontFamily", commTextFontFamily);
        settings->setValue("/text/commTextFontSize", commTextFontSize);
        settings->setValue("/tip/tipTextFontFamily", tipTextFontFamily);
        settings->setValue("/tip/tipTextFontSize", tipTextFontSize);
        // app setting
        settings->setValue("/app/autoupdate", isAutoUpdate);
        settings->setValue("/app/themename", themeName);
        settings->setValue("/app/state", state);
        settings->setValue("/app/guid", guid);
        settings->setValue("/app/saysomethingtime", saySomethingTime);
        settings->setValue("/app/clipboardmonitor", enableClipboardMonitor);

        // bible settings
        settings->setValue("/bible/isShowXref", isShowXref);
        settings->setValue("/bible/verseCopyTitleStyle", verseCopyTitleStyle);

        delete settings;
    }
}
QString BibleReaderConfigurator::getBiblePathBase() const
{
    return biblePathBase;
}

void BibleReaderConfigurator::setBiblePathBase(const QString &value)
{
    biblePathBase = value;
}
QString BibleReaderConfigurator::getDefaultBibleVersion() const
{
    return defaultBibleVersion;
}

void BibleReaderConfigurator::setDefaultBibleVersion(const QString &value)
{
    defaultBibleVersion = value;
}
int BibleReaderConfigurator::getLastBook() const
{
    return lastBook;
}

void BibleReaderConfigurator::setLastBook(int value)
{
    lastBook = value;
}
int BibleReaderConfigurator::getLastChapter() const
{
    return lastChapter;
}

void BibleReaderConfigurator::setLastChapter(int value)
{
    lastChapter = value;
}
int BibleReaderConfigurator::getLastVerse() const
{
    return lastVerse;
}

void BibleReaderConfigurator::setLastVerse(int value)
{
    lastVerse = value;
}

QString BibleReaderConfigurator::getDictPathBase() const
{
    return dictPathBase;
}

void BibleReaderConfigurator::setDictPathBase(QString &value)
{
    dictPathBase = value;
}

QString BibleReaderConfigurator::getDefaultDict() const
{
    return defaultDict;
}

void BibleReaderConfigurator::setDefaultDict(QString &value)
{
    defaultDict = value;
}

QString BibleReaderConfigurator::getDefaultCommentary() const
{
    return defaultCommentary;
}

void BibleReaderConfigurator::setDefaultCommentary(const QString &value)
{
    defaultCommentary = value;
}

QString BibleReaderConfigurator::getBcPathBase() const
{
    return bcPathBase;
}

void BibleReaderConfigurator::setBcPathBase(const QString &value)
{
    bcPathBase = value;
}

QColor BibleReaderConfigurator::getSelectedVerseBG() const
{
    return selectedVerseBG;
}

void BibleReaderConfigurator::setSelectedVerseBG(const QColor &value)
{
    if (selectedVerseBG != value) {
        selectedVerseBG = value;
        emit selectedVerseBGChanged(value);
    }
}

QColor BibleReaderConfigurator::getProjectDlgBG() const
{
    return projectDlgBG;
}

void BibleReaderConfigurator::setProjectDlgBG(const QColor &value)
{
    if (projectDlgBG != value) {
        projectDlgBG = value;
        emit projectDlgBGChanged(value);
    }

}

QColor BibleReaderConfigurator::getProjectDlgFG() const
{
    return projectDlgFG;
}

void BibleReaderConfigurator::setProjectDlgFG(const QColor &value)
{
    if (projectDlgFG != value) {
        projectDlgFG = value;
        emit projectDlgFGChanged(value);
    }
}

bool BibleReaderConfigurator::getIsAutoUpdate() const
{
    return isAutoUpdate;
}

void BibleReaderConfigurator::setIsAutoUpdate(bool value)
{
    isAutoUpdate = value;
}

QString BibleReaderConfigurator::getBibleTextFontFamily() const
{
    return bibleTextFontFamily;
}

void BibleReaderConfigurator::setBibleTextFontFamily(const QString &value)
{
    if (bibleTextFontFamily != value) {
        bibleTextFontFamily = value;
        emit bibleTextFontFamilyChanged(value);
    }

}

double BibleReaderConfigurator::getBibleTextFontSize() const
{
    return bibleTextFontSize;
}

void BibleReaderConfigurator::setBibleTextFontSize(double value)
{
    if (bibleTextFontSize != value) {
        bibleTextFontSize = value;
        emit bibleTextFontSizeChanged(value);
    }

}

QString BibleReaderConfigurator::getBxPathBase() const
{
    return bxPathBase;
}

void BibleReaderConfigurator::setBxPathBase(const QString &value)
{
    bxPathBase = value;
}

QString BibleReaderConfigurator::getLayoutPath() const
{
    return layoutPath;
}

void BibleReaderConfigurator::setLayoutPath(const QString &value)
{
    layoutPath = value;
}

QString BibleReaderConfigurator::getThemeName() const
{
    return themeName;
}

void BibleReaderConfigurator::setThemeName(const QString &value)
{
    themeName = value;
}

QByteArray BibleReaderConfigurator::getState() const {
    return state;
}

void BibleReaderConfigurator::setState(const QByteArray &value) {
    state = value;
}

QString BibleReaderConfigurator::getGuid() const
{
    return guid;
}

void BibleReaderConfigurator::setGuid(const QString &value)
{
    guid = value;
}

QString BibleReaderConfigurator::getCommTextFontFamily() const
{
    return commTextFontFamily;
}

void BibleReaderConfigurator::setCommTextFontFamily(const QString &value)
{
    if (commTextFontFamily != value) {
        commTextFontFamily = value;
        emit commTextFontFamilyChanged(value);
    }
}

double BibleReaderConfigurator::getCommTextFontSize() const
{
    return commTextFontSize;
}

void BibleReaderConfigurator::setCommTextFontSize(double value)
{
    if (commTextFontSize != value) {
        commTextFontSize = value;
        emit commTextFontSizeChanged(value);
    }
}

bool BibleReaderConfigurator::getIsShowXref() const
{
    return isShowXref;
}

void BibleReaderConfigurator::setIsShowXref(bool value)
{
    isShowXref = value;
    emit isShowXrefChanged(value);
}

int BibleReaderConfigurator::getVerseCopyTitleStyle() const
{
    return verseCopyTitleStyle;
}

void BibleReaderConfigurator::setVerseCopyTitleStyle(int value)
{
    verseCopyTitleStyle = value;
    emit verseCopyTitleStyleChanged(value);
}

QString BibleReaderConfigurator::getPrjTextFontFamily() const
{
    return prjTextFontFamily;
}

void BibleReaderConfigurator::setPrjTextFontFamily(const QString &value)
{
    prjTextFontFamily = value;
}

double BibleReaderConfigurator::getPrjTextFontSize() const
{
    return prjTextFontSize;
}

void BibleReaderConfigurator::setPrjTextFontSize(double value)
{
    prjTextFontSize = value;
}

int BibleReaderConfigurator::getSaySomethingTime() const
{
    return saySomethingTime;
}

void BibleReaderConfigurator::setSaySomethingTime(int value)
{
    saySomethingTime = value;
}

QString BibleReaderConfigurator::getTipTextFontFamily() const
{
    return tipTextFontFamily;
}

void BibleReaderConfigurator::setTipTextFontFamily(const QString &value)
{
    tipTextFontFamily = value;
}

double BibleReaderConfigurator::getTipTextFontSize() const
{
    return tipTextFontSize;
}

void BibleReaderConfigurator::setTipTextFontSize(double value)
{
    tipTextFontSize = value;
}

bool BibleReaderConfigurator::getEnableClipboardMonitor() const
{
    return enableClipboardMonitor;
}

void BibleReaderConfigurator::setEnableClipboardMonitor(bool value)
{
    enableClipboardMonitor = value;
    emit clipboardMonitorChanged(value);
}

QString BibleReaderConfigurator::getBePathBase() const
{
    return bePathBase;
}

void BibleReaderConfigurator::setBePathBase(const QString &value)
{
    bePathBase = value;
}






